package api

import (
	"api_client/basic/config"
	__ "api_client/basic/proto"
	"api_client/handler/request"
	"api_client/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func SendCode(c *gin.Context) {
	var req request.SendCodeReq
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "发送成功",
		})
		return
	}
	r, err := config.Client.SmsMessage(c, &__.SmsMessageReq{
		Mobile: req.Mobile,
		Code:   uint32(req.Code),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": r.Coe,
			"msg":  r.Msg,
		})
		return
	}
	pkg.SendCode(req.Mobile, strconv.Itoa(int(req.Code)))
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发送成功",
	})
}

func Register(c *gin.Context) {
	var req request.RegisterReq
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "注册成功",
		})
		return
	}
	r, err := config.Client.Register(c, &__.RegisterReq{
		Username: req.Username,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": r.Coe,
			"msg":  r.Msg,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "注册成功",
	})
}

func Login(c *gin.Context) {
	var req request.LoginReq
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "登录成功",
		})
		return
	}
	r, err := config.Client.Login(c, &__.LoginReq{
		Username: req.Username,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": r.Coe,
			"msg":  r.Msg,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
}
